<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<?php
$doc = new DOMDocument("1.0", "utf-8");

$doc->load("http://localhost/webservices/test.xml");

$node = $doc->documentElement;
$root = $doc->documentElement;

switch ($node->nodeType) {
	case XML_ELEMENT_NODE:
		print "Element Tag Name: ".$node->nodeName;
		if ($node->nodeName == "book") {
			/* We may want the lang attribute */
		}
		break;
	case XML_ATTRIBUTE_NODE:
		print "Attribute Name: ".$node->nodeName."\n";
		print "Attribute Value: ".$node->nodeValue."\n";
		if ($node->nodeName == "lang") {
			/* Do something with the language */
		}
		break;
	case XML_TEXT_NODE:
	case XML_CDATA_SECTION_NODE:
		print "Content: ".$node->nodeValue."\n";
		break;
	default:
		print "Other Node Names: ".$node->nodeName."\n";
}
echo "<br/>================================<br/>";
echo "Duyệt qua tất cả các node<br/>";
if ($root->hasChildNodes()) {
	$children = $root->childNodes;
	foreach($children as $node) {
		print $node->nodeName."\n";
	}
}

echo "<br/>================================<br/>";
echo "get last node & firstnode<br/>";
$firstNode = $root->firstChild;
echo "First child ".$firstNode->nodeName."<br/>";

$lastNode = $root->lastChild;
echo "Last child".$lastNode->nodeName;


echo "<br/>================================<br/>";
echo "have sample parent! nextSibling<br/>";

$node = $firstNode;
while($node){
	if($node->nodeType == XML_ELEMENT_NODE){
		echo $node->nodeName."<br/>";
	}

	$node = $node->nextSibling;
}


echo "<br/>================================<br/>";
echo "have sample parent! nextSibling<br/>";

$node = $lastNode;
while($node){
	if($node->nodeType == XML_ELEMENT_NODE){
		echo $node->nodeName."<br/>";
	}

	$node = $node->previousSibling;
}


echo "<br/>================================<br/>";
echo "parent node<br/>";

do{

	$node = $firstNode;
	while($node){
		if(!$node->parentNode->isSameNode($root)){
			echo "ERROR: Parent Node Test FAILED<br/>";
			break 2;
		}
		$node = $node->nextSibling;
	}

}while(0);

echo "<br/>================================<br/>";
echo "getElementByTagName<br/>";

$elements = $doc->getElementsByTagName('child');
for($i=0;$i < $elements->length; $i++){
	echo $elements->item($i)->nodeName."<br/>";
}

echo "<br/>================================<br/>";
echo "List all attributes<br/>";
if($root->hasAttributes()){
	$attributes = $root->attributes;
	foreach($attributes as $attr){
		echo "Attribute name = ".$attr->nodeName."<br/>";
		echo "";
	}
}

echo "<br/>================================<br/>";
echo "Delete all nodes have condition<br/>";
$children = $root->childNodes;

for($x = $children->length-1; $x>=0; $x--){
	$node = $children->item($x);
	if($node->nodeType == XML_TEXT_NODE){
		$root->removeChild($node);
	}
}

if($root->hasChildNodes()){
	$childrens = $root->childNodes;
	foreach($children as $child){
		echo $child->nodeName."<br/>";
	}
}
